package mfuny.ggongal.admin.http;

import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.ad.AdDao;
import mfuny.ggongal.admin.ad.AdDetail;
import mfuny.ggongal.admin.ad.MissionDetail;
import mfuny.ggongal.admin.etc.Module;
import mfuny.ggongal.admin.gift.GiftCardDao;
import mfuny.ggongal.admin.gift.GiftDao;
import mfuny.ggongal.admin.json.JsonCreater;
import mfuny.ggongal.admin.play.PlayService;
import mfuny.ggongal.admin.timetable.TimeDao;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.ibatis.sqlmap.client.SqlMapClient;

public class HttpAd {
	SqlMapClient sqlMapper;
	
	public HttpAd() throws Exception {
		sqlMapper = Server.getSqlMapper( );
	}
	
	//오늘의 꽁게임 기본정보.
	@SuppressWarnings({ "deprecation", "unchecked" })
	public JSONObject ad_ggongToday() throws Exception {
		JSONObject json = new JSONObject();
		JsonCreater jc = new JsonCreater();
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		Date date = new Date();
		paramMap.put( "day", "d" + date.getDate( ) );
		paramMap.put( "year", date.getYear( ) + 1900  );
		paramMap.put( "month", date.getMonth( ) + 1 );
		String days = (String)jc.getDao( "gday_adNoForDay", paramMap );
		
		if ( days == null ) {
			json.put("responseCode", 0);
			return json;
		}
		
		String[] day = days.split( "," );
		
		List<Integer> dayList = new ArrayList<Integer>();
		for( int i = 0; i < day.length; i++ ) {
			dayList.add( Integer.parseInt( day[i] ) );
		}
		
		if ( dayList.size( ) <= 0 ) {
			json.put("responseCode", 0);
			return json;
		}
		
		json = jc.getJsonByMapList( jc.getDaoList( "ad_ggongToday", dayList ), "ggongToday" );
		json.put("responseCode", 1);
		return json;
	}
	
	
	//현재시간부터의 오늘의 타임게임 기본정보.
	@SuppressWarnings( { "unchecked", "deprecation" } )
	public JSONObject ad_timeToday() throws Exception {
		JSONObject json = new JSONObject();
		JsonCreater jc = new JsonCreater();
		Timestamp ts = new Timestamp(System.currentTimeMillis( ));
		String day = new SimpleDateFormat("yyyy-MM-dd").format( ts );
		int nowHour = ts.getHours( );
		List<Integer> ad_no = new ArrayList<Integer>();
		
		TimeDao timeClass = (TimeDao) sqlMapper.queryForObject( "time_selectOne", day );
		
		if ( timeClass == null ) {
			json.put("responseCode", 0);
			return json;
		}
		
		Method[] m = timeClass.getClass( ).getMethods( );
		
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		
		for ( int i = 0; i < m.length; i++ ) {
			String methodName = m[i].getName( );
			if( methodName.substring( 0, 4 ).equals( "getH" ) && m[i].invoke( timeClass ) != null ) {
				int methodHour = Integer.parseInt( methodName.substring( 4 ) );
				if ( methodHour >= nowHour ) {
					paramMap.put( "ad_no", Integer.parseInt((String)m[i].invoke( timeClass )) );
					paramMap.put( "hour" + paramMap.get( "ad_no" ), methodHour );
					ad_no.add( (Integer)paramMap.get( "ad_no" ) );
				}
			}
		}
		
		if ( ad_no.size( ) <= 0 ) {
			json.put("responseCode", 0);
			return json;
		}
		
		List<Object> resultMap = jc.getDaoList( "ad_timeToday", ad_no );
		
		if ( resultMap.size( ) <= 0 ) {
			json = jc.getNullJsonList( "timeToday" );
			return json;
		}
		
		for ( int i = 0; i < resultMap.size( ); i++ ) {
			HashMap<String, Object> map = (HashMap<String, Object>)resultMap.get( i );
			map.put( "hour", paramMap.get( "hour"+ map.get( "ad_no" ) ) );
			
			String mission = (String)map.get( "mission" );
			String[ ] missionDetail = mission.split( "/" )[ 2 ].split( "\\|" )[ 0 ].split( "," );
			String gift = null;
			if ( missionDetail[ 2 ].equals( "현물" ) ) {
				gift = (String)sqlMapper.queryForObject( "gift_selectGiftName", missionDetail[ 3 ] );
				map.put( "gift", gift );
			} else if ( missionDetail[ 2 ].equals( "모바일상품권" ) ) {
				gift = (String)sqlMapper.queryForObject("card_selectCardName", Integer.parseInt(missionDetail[ 3 ] + ( missionDetail.length > 4 ? "/" + missionDetail[ 4 ] : "")));
				map.put( "gift", gift );
			}
			
			resultMap.set( i, map );
		}
		json = jc.getJsonByMapList( resultMap, "timeToday" );
		json.put("responseCode", 1);
		return json;
	}
	
	// 현재시간부터의 오늘의 스페셜게임 기본정보.
	@SuppressWarnings( { "unchecked", "deprecation" } )
	public JSONObject ad_specialToday( ) throws Exception {
		JSONObject json = new JSONObject( );
		JsonCreater jc = new JsonCreater( );
		Timestamp ts = new Timestamp( System.currentTimeMillis( ) );
		String day = new SimpleDateFormat( "yyyy-MM-dd" ).format( ts );
		int nowHour = ts.getHours( );
		List<Integer> ad_no = new ArrayList<Integer>( );

		TimeDao timeClass = (TimeDao)sqlMapper.queryForObject( "time_selectOne", day );

		if ( timeClass == null ) {
			json.put("responseCode", 0);
			return json;
		}

		Method[ ] m = timeClass.getClass( ).getMethods( );

		HashMap<String, Object> paramMap = new HashMap<String, Object>( );

		for ( int i = 0; i < m.length; i++ ) {
			String methodName = m[ i ].getName( );
			if ( methodName.substring( 0, 4 ).equals( "getH" ) && m[ i ].invoke( timeClass ) != null ) {
				
				//현재 시간 이후의 경품게임들. ( 버전코드 20 이전 )
//				int methodHour = Integer.parseInt( methodName.substring( 4 ) );
//				if ( methodHour >= nowHour ) {
//					paramMap.put( "ad_no", Integer.parseInt( (String)m[ i ].invoke( timeClass ) ) );
//					paramMap.put( "hour" + paramMap.get( "ad_no" ), methodHour );
//					ad_no.add( (Integer)paramMap.get( "ad_no" ) );
//				}
				
				//오늘 전체 경품게임들. ( 버전코드 21 이후 )
				int methodHour = Integer.parseInt( methodName.substring( 4 ) );
				paramMap.put( "ad_no", Integer.parseInt( (String)m[ i ].invoke( timeClass ) ) );
				paramMap.put( "hour" + paramMap.get( "ad_no" ), methodHour );
				ad_no.add( (Integer)paramMap.get( "ad_no" ) );
			}
		}

		if ( ad_no.size( ) <= 0 ) {
			json.put("responseCode", 0);
			return json;
		}

		List<Object> resultMap = jc.getDaoList( "ad_specialToday", ad_no );
		
		if ( resultMap.size( ) <= 0 ) {
			json.put("responseCode", 0);
			return json;
		}
		
		for ( int i = 0; i < resultMap.size( ); i++ ) {
			HashMap<String, Object> map = (HashMap<String, Object>)resultMap.get( i );
			map.put( "hour", paramMap.get( "hour" + map.get( "ad_no" ) ) );
			
			String mission = (String)map.get( "mission" );
			String[ ] missionDetail = mission.split( "/" )[ 2 ].split( "\\|" )[ 0 ].split( "," );
			String gift = null;
			if ( missionDetail[ 2 ].equals( "현물" ) ) {
				gift = (String)sqlMapper.queryForObject( "gift_selectGiftName", missionDetail[ 3 ] );
				map.put( "gift", gift );
			} else if ( missionDetail[ 2 ].equals( "모바일상품권" ) ) {
				gift = (String)sqlMapper.queryForObject("card_selectCardName", Integer.parseInt(missionDetail[ 3 ] + ( missionDetail.length > 4 ? "/" + missionDetail[ 4 ] : "")));
				map.put( "gift", gift );
			}
			
			resultMap.set( i, map );
		}
		json = jc.getJsonByMapList( resultMap, "specialToday" );
		json.put("responseCode", 1);
		return json;
	}
	
	//오늘의 경품게임 알람리스트.
	@SuppressWarnings( { "unchecked" } )
	public JSONObject ad_alarmToday() throws Exception {
		JSONObject json = new JSONObject( );
		JsonCreater jc = new JsonCreater( );
		Timestamp ts = new Timestamp( System.currentTimeMillis( ) );
		String day = new SimpleDateFormat( "yyyy-MM-dd" ).format( ts );
//		int nowHour = ts.getHours( );
		List<Integer> ad_no = new ArrayList<Integer>( );

		TimeDao timeClass = (TimeDao)sqlMapper.queryForObject( "time_selectOne", day );

		if ( timeClass == null ) {
			json = jc.getNullJsonList( "alarmToday" );
			return json;
		}

		Method[ ] m = timeClass.getClass( ).getMethods( );

		HashMap<String, Object> paramMap = new HashMap<String, Object>( );

		for ( int i = 0; i < m.length; i++ ) {
			String methodName = m[ i ].getName( );
			if ( methodName.substring( 0, 4 ).equals( "getH" ) && m[ i ].invoke( timeClass ) != null ) {
				int methodHour = Integer.parseInt( methodName.substring( 4 ) );
//				if ( methodHour >= nowHour ) {
					paramMap.put( "ad_no", Integer.parseInt( (String)m[ i ].invoke( timeClass ) ) );
					paramMap.put( "hour" + paramMap.get( "ad_no" ), methodHour );
					ad_no.add( (Integer)paramMap.get( "ad_no" ) );
//				}
			}
		}

		if ( ad_no.size( ) <= 0 ) {
			json = jc.getNullJsonList( "alarmToday" );
			return json;
		}

		List<Object> resultMap = jc.getDaoList( "ad_alarmToday", ad_no );
		
		if ( resultMap.size( ) <= 0 ) {
			json = jc.getNullJsonList( "alarmToday" );
			return json;
		}
		
		for ( int i = 0; i < resultMap.size( ); i++ ) {
			HashMap<String, Object> map = (HashMap<String, Object>)resultMap.get( i );
			map.put( "hour", paramMap.get( "hour" + map.get( "ad_no" ) ) );
//			String mission = (String)map.get( "mission" );
//			
//			String[] missionDetail = mission.split( "/" )[2].split( "\\|" )[0].split( "," );
//			
//			String gift = null;
//			if ( missionDetail[2].equals( "현물" )) {
//				gift = (String)sqlMapper.queryForObject( "gift_selectGiftName", missionDetail[3] );
//				map.put( "gift", gift );
//			} else if ( missionDetail[2].equals( "모바일상품권" )) {
//				gift = (String)sqlMapper.queryForObject("card_selectCardName", Integer.parseInt(missionDetail[ 3 ] + ( missionDetail.length > 4 ? "/" + missionDetail[ 4 ] : "")));
//				map.put( "gift", gift );
//			}
			map.remove( "mission" );
			resultMap.set( i, map );
		}
		
		json = jc.getJsonByMapList( resultMap, "alarmToday" );
		return json;
	}
	
	//광고번호로 특정 광고정보 가져오기(한개).
	@SuppressWarnings( "unchecked" )
	public JSONObject ad_oneAd(int ad_no) throws Exception {
		JSONObject json = new JSONObject();
		JsonCreater jc = new JsonCreater();
		HashMap<String, Object> resultMap = (HashMap<String, Object>)jc.getDao( "ad_selectAllOne", ad_no );
		
		String mission = (String)resultMap.get( "mission" );
		String[] missionDetail = mission.split( "/" )[2].split( "\\|" )[0].split( "," );
		
		if(!((String)resultMap.get("service")).equals("ggong")) {
			String gift = null;
			if ( missionDetail[2].equals( "현물" )) {
				gift = (String) sqlMapper.queryForObject( "gift_selectGiftName", missionDetail[3] );
			} else if ( missionDetail[2].equals( "모바일상품권" )) {
				gift = (String)sqlMapper.queryForObject("card_selectCardName", Integer.parseInt(missionDetail[ 3 ] + ( missionDetail.length > 4 ? "/" + missionDetail[ 4 ] : "")));
			}
			resultMap.put( "gift", gift );
		}
		
		json = jc.getJsonByMap( resultMap );
		
		return json;
	}
	
	//광고번호로 특정 광고정보 가져오기(다중).
	@SuppressWarnings( "unchecked" )
	public JSONObject ad_adsInfo(String ads) throws Exception {
		JSONObject json = new JSONObject();
		JsonCreater jc = new JsonCreater();
		
		List<String> ad_no = new ArrayList<String>();
		String[] ad = ads.split( "," );
		for ( int i = 0; i < ad.length; i++ ) {
			ad_no.add( ad[i] );
		}
		
		List<Object> resultMap = jc.getDaoList( "ad_selectByAdNos", ad_no );
		for ( int i = 0; i < resultMap.size( ); i++ ) {
			HashMap<String, Object> map = (HashMap<String, Object>)resultMap.get( i );;
			String mission = (String)map.get( "mission" );
			
			String[] missionDetail = mission.split( "/" )[2].split( "\\|" )[0].split( "," );
			
			String gift = null;
			if ( missionDetail[2].equals( "현물" )) {
				gift = (String)sqlMapper.queryForObject( "gift_selectGiftName", missionDetail[3] );
			} else if ( missionDetail[2].equals( "모바일상품권" )) {
				gift = (String)sqlMapper.queryForObject("card_selectCardName", Integer.parseInt(missionDetail[ 3 ] + ( missionDetail.length > 4 ? "/" + missionDetail[ 4 ] : "")));
			}
			map.put( "gift", gift );
			resultMap.set( i, map );
		}
		json = jc.getJsonByMapList( resultMap, "adsInfo" );
		if( resultMap.size() > 0 ) {
			json.put("responseCode", 1);
		} else {
			json.put("responseCode", 0);
		}
		
		return json;
	}
	
	//광고번호로 경품상세정보 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject ad_getGiftDetail(int ad_no) throws Exception {
		AdDao ad = (AdDao) sqlMapper.queryForObject("ad_selectNo", ad_no);
		String mission = ad.getMission();
		List<Object> giftIds = MissionDetail.getGiftIdList_mission(mission, "현물");
		List<Object> giftcardNos = MissionDetail.getGiftIdList_mission(mission, "모바일상품권");
		
		List<GiftDao> gifts = null;
		List<GiftCardDao> giftcards = null;
		JSONObject json = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		if(giftIds.size() > 0) {
			gifts = sqlMapper.queryForList("gift_selectByListId", giftIds);
			if(gifts != null && gifts.size() > 0) {
				for(int i = 0; i < gifts.size(); i++) {
					GiftDao gift = gifts.get(i);
					JSONObject jsonTemp = new JSONObject();
					jsonTemp.put("giftType", "현물");
					jsonTemp.put("id", gift.getId());
					jsonTemp.put("img_savname", gift.getImg_savname());
					jsonTemp.put("name", gift.getName());
					jsonTemp.put("price", gift.getPrice());
					jsonArray.add(jsonTemp);
				}
			}
		}
		if(giftcardNos.size() > 0) {
			giftcards = sqlMapper.queryForList("card_selectByListNo", giftcardNos);
			if(giftcards != null && giftcards.size() > 0) {
				for(int i = 0; i < giftcards.size(); i++) {
					GiftCardDao giftcard = giftcards.get(i);
					JSONObject jsonTemp = new JSONObject();
					jsonTemp.put("giftType", "모바일상품권");
					jsonTemp.put("id", giftcard.getCard_no());
					jsonTemp.put("img_savname", giftcard.getCard_img_savname());
					jsonTemp.put("name", giftcard.getCard_name());
					jsonTemp.put("price", giftcard.getCard_price());
					jsonArray.add(jsonTemp);
				}
			}
		}
		
		json.put("gifts", jsonArray);
		
//		JSONObject json = new JsonCreater().getJsonByList(sqlMapper.queryForList("gift_selectByAdNo", ad_no), "gifts");
		return json;
	}
	
	//광고번호로 경품정보 가져오기 (등수 포함).
	@SuppressWarnings("unchecked")
	public JSONObject ad_getGiftDetailWithRank(int ad_no) throws Exception {
		JSONObject json = null;
		AdDao ad = (AdDao) sqlMapper.queryForObject("ad_selectNo", ad_no);
		String[] gifts = ad.getMission().split("/")[2].split("\\|");
		
		List<String> sqlList = new ArrayList<String>();
		for( int i = 0; i < gifts.length; i++ ) {
			String[] gift = gifts[i].split(",");
			String sql = "";
			if( gift[2].equals("현물") ) {
				sql = "select if(" + gift[0] + " - " + gift[1] + " < 0,concat(" + gift[0] + ", \'~\', " + gift[1] + "), " + gift[0] + ") as rank, gift.name as gift_name, gift.img_savname as gift_img, gift.img_capacity as gift_img_capacity from gift where id = \'" + gift[3] + "\'";
			} else {
				sql = "select if(" + gift[0] + " - " + gift[1] + " < 0,concat(" + gift[0] + ", \'~\', " + gift[1] + "), " + gift[0] + ") as rank, giftcard.card_name as gift_name, giftcard.card_img_savname as gift_img, giftcard.card_img_capacity as gift_img_capacity from giftcard where card_no = " + gift[3];
			}
			sqlList.add(sql);
		}
		
		JsonCreater jc = new JsonCreater();
		List<Object> giftList = sqlMapper.queryForList("ad_getGiftDetailWithRank", sqlList);
		if( giftList.size() > 0 ) {
			json = jc.getJsonByMapList(giftList, "giftList");
			json.put("responseCode", 1);
		} else {
			json = new JSONObject();
			json.put("responseCode", 0);
		}
		
		return json;
	}
	
	
	//실시간 랭킹보기 (vc-23).
		@SuppressWarnings({ "deprecation", "unchecked" })
		public JSONObject ad_getRank(String id, int ad_no, int start, String days) throws Exception {
			JSONObject json = null;
			
			//해당광고.
			AdDao ad = (AdDao)sqlMapper.queryForObject("ad_selectNo", ad_no);
			
			//해당 광고 당첨등수리스트.
			List<Integer> winNumList = MissionDetail.getWinRankNum_mission(ad.getMission());
			
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			Date starttime = AdDetail.ad_getDateForSGame(ad);
			
			starttime.setMinutes(0);
			starttime.setSeconds(0);
			
			paramMap.put("ad_no", ad_no);
			paramMap.put("starttime", starttime);
			paramMap.put("start", start);
			paramMap.put("id", id);
			paramMap.put("count", 100);
			paramMap.put("winNumList", winNumList);
			paramMap.put("hour", AdDetail.vc27Hour);
			paramMap.put("minute", AdDetail.vc27Minute);
			
			List<Object> rankList = sqlMapper.queryForList("play_getRankSpecial27", paramMap);
			if( rankList.size() > 0 ) {
				json = new JsonCreater().getJsonByMapList(rankList, "rankList");
				json.put("responseCode", 1);
				json.put("game_no", ad.getGame_no());
				
				HashMap<String, Object> myRankMap = (HashMap<String, Object>) sqlMapper.queryForObject("play_getRankByIdForSpecial27", paramMap);
				Object myRank = myRankMap != null ? myRankMap.get("rank") : null;
				if ( myRank != null ) {
					json.put("myRank", myRank );
					json.put("myScore", myRankMap.get("score"));
					json.put("myPicupdate", myRankMap.get("picupdate"));
					json.put("myGameendtime", String.valueOf(myRankMap.get("gameendtime")));
				} else {
					json.put("myRank", 0);
				}
			} else {
				json = new JSONObject();
				json.put("responseCode", 0);
			}
			
			return json;
		}
		
	// 게임에서 요청하는 랭킹. 최대 50개.
	@SuppressWarnings({ "deprecation", "unchecked" })
	public JSONObject ad_getRankForGameEnd(String id, int ad_no, int start, String days) throws Exception {
		JSONObject json = null;

		// 해당광고.
		AdDao ad = (AdDao) sqlMapper.queryForObject("ad_selectNo", ad_no);
		
		// 해당 광고 당첨등수리스트.
		List<Integer> winNumList = MissionDetail.getWinRankNum_mission(ad.getMission());

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		Date starttime = AdDetail.ad_getDateForSGame(ad);

		starttime.setMinutes(0);
		starttime.setSeconds(0);

		paramMap.put("ad_no", ad_no);
		paramMap.put("starttime", starttime);
		paramMap.put("start", start);
		paramMap.put("id", id);
		paramMap.put("count", 50);
		paramMap.put("winNumList", winNumList);
		paramMap.put("hour", AdDetail.vc27Hour);
		paramMap.put("minute", AdDetail.vc27Minute);

		List<Object> rankList = sqlMapper.queryForList("play_getRankSpecial27", paramMap);
		if (rankList.size() > 0) {
			json = new JsonCreater().getJsonByMapList(rankList, "rankList");
			json.put("responseCode", 1);
			json.put("game_no", ad.getGame_no());

			HashMap<String, Object> myRankMap = (HashMap<String, Object>) sqlMapper.queryForObject(
					"play_getRankByIdForSpecial27", paramMap);
			Object myRank = myRankMap != null ? myRankMap.get("rank") : null;
			if (myRank != null) {
				json.put("myRank", myRank);
				json.put("myScore", myRankMap.get("score"));
				json.put("myPicupdate", myRankMap.get("picupdate"));
				json.put("myGameendtime", String.valueOf(myRankMap.get("gameendtime")));
			} else {
				json.put("myRank", 0);
			}
		} else {
			json = new JSONObject();
			json.put("responseCode", 0);
		}

		return json;
	}
		
	
	//스페셜 게임에 해당 휴대폰번호로 플레이 한 경우 진입 제한.(vc-27)
	@SuppressWarnings({ "deprecation", "unchecked" })
	public JSONObject ad_checkSGame(String id, int ad_no, int hour) throws Exception {
		JSONObject json = new JSONObject();
		Date now = new Date();
		
		AdDao ad = (AdDao)sqlMapper.queryForObject("ad_selectNo", ad_no);
		Date starttime = AdDetail.ad_getDateForSGame(ad);
		
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		paramMap.put("ad_no", ad_no);
		paramMap.put("starttime", starttime);
		paramMap.put("hour", AdDetail.vc27Hour);
		paramMap.put("minute", AdDetail.vc27Minute);
		
		
		//해당 게임에 같은 번호 다른 아이디 플레이 기록 count  및 hour 구하기.
		int count = (int)(double)(Double.parseDouble(String.valueOf(sqlMapper.queryForObject("play_otherMyIdCount", paramMap))));
		
		// 같은 번호 다른 아이디 플레이가 있으면 진입 불가.
		if (count > 0) {
			json.put("responseCode", -2);
			json.put("msg", "다른 아이디로 플레이 하였습니다.. count : " + count);
			return json;
		}
				
		//유저가 올바른 시간에 게임을 진입하는가?
		if( now.getHours() < hour || now.getHours() > hour+2 ) {
			json.put("responseCode", -3);
			json.put("msg", "시간오류 : " + now.getHours() + " / " + hour);
			return json;
		}
		
		json.put("responseCode", 1);
		
		return json;
	}
	
	//전일 당첨자 리스트.
	@SuppressWarnings({ "deprecation", "unchecked" })
	public JSONObject ad_getYesterdayWinner() throws Exception {
		JSONObject json = new JSONObject();
		
		//현재 시간이 00시 05분 이전이면 리스트 안줌.
		Date today = new Date();
		if (today.getHours() <= 0 && today.getMinutes() <= 5) {
			json.put("responseCode", 0);
			return json;
		}
		
		Date yesterday = new Date();
		yesterday.setDate(yesterday.getDate() - 1);
		String dayStr = new SimpleDateFormat("yyyy-M-d").format(yesterday);
		String yesAd_nos = (String)sqlMapper.queryForObject("time_selectGetAdNos", dayStr);
		
		if( yesAd_nos == null || yesAd_nos.equals("")) {
			json.put("responseCode", 0);		//전일 경품게임이 전혀 없는경우.
			return json;
		}
		
		yesAd_nos = yesAd_nos.substring(0, yesAd_nos.length() - 1);
		
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("days", dayStr);
		paramMap.put("ad_nos", yesAd_nos);
		List<Object> resultList = (List<Object>) sqlMapper.queryForList("ad_yesterdayAd", paramMap);
		
		if( resultList.size() <= 0 ) {
			json.put("responseCode", -1);		//리스트 없음.
			return json;
		}
		
		PlayService play = new PlayService();
		for(int i = 0; i < resultList.size(); i++ ) {
			HashMap<String, Object> adMap = (HashMap<String, Object>) resultList.get(i);
			List<Object> winnerList = play.getWinnerForAd((Integer)adMap.get("ad_no"), Module.strToDate(dayStr + " " + adMap.get("hour") + ":00:00"));
			adMap.put("winnerList", winnerList);
			resultList.set(i, adMap);
		}
		
		json = new JsonCreater().getJsonByMapList(resultList, "yesterdayWinnder");
		json.put("responseCode", 1);
		
		return json;
	}
	
	//오늘 필요한 모든 이미지&용량 가져오기.
	@SuppressWarnings("unchecked")
	public JSONObject ad_getImgToday() throws Exception {
		JSONObject json = null;
		List<Object> imgList = sqlMapper.queryForList("ad_getImgToday", null);
		
		if( imgList.size() > 0 ) {
			json = new JsonCreater().getJsonByMapList(imgList, "imgList");
			json.put("responseCode", 1);
		} else {
			json = new JSONObject();
			json.put("responseCode", 0);
		}
		
		return json;
	}
}
